package demo.demo10;

import java.lang.annotation.*;
import java.lang.reflect.Field;

public class test12 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> aClass = Class.forName("demo.demo10.test12");

        //通过反射获得注解
        Annotation[] annotations = aClass.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        //获得注解的值
        Tablezyh annotation1 = (Tablezyh) aClass.getAnnotation(Tablezyh.class);
        String value = annotation1.value();
        System.out.println(value);
        
        //指定类的注解
        Field names = aClass.getDeclaredField("name");
        Fieldzyh annotation = names.getAnnotation(Fieldzyh.class);
        System.out.println(annotation.columnName());
        System.out.println(annotation.type());
        System.out.println(annotation.length());

    }
}

@Tablezyh("db_zyh")
class Student2 {
    @Fieldzyh(columnName = "db_id", type = "int", length = 10)
    private int id;
    @Fieldzyh(columnName = "db_age", type = "int", length = 10)
    private int age;
    @Fieldzyh(columnName = "db_name", type = "varchar", length = 3)
    private String name;

    public Student2() {
    }

    public Student2(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student2{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

//类名注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Tablezyh {
    String value();
}

//属性的注解
@interface Fieldzyh {
    String columnName();

    String type();

    int length();
}